﻿namespace Microsoft.Services.TestTools.UITesting.Html {
    using System;
    using System.Reflection;

    /// <summary>
    /// <see cref="Object"/> extension methods
    /// </summary>
    public static class ObjectExtensions {

        /// <summary>
        /// Executes the provided <see cref="Action"/> for each public property on the object.
        /// It passes the name and the value of the property to the action.
        /// </summary>
        /// <param name="self">The self.</param>
        /// <param name="action">The action.</param>
        public static void ForEachProperty(this object self, Action<string, string> action) {
            var props = self.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var prop in props) {
                var val = prop.GetValue(self, null).ToString();
                action(prop.Name, val);
            }
        }
    }
}